package com.tengju.settlement.domain.shared;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Objects;


/**
 * 金额
 * <p>
 */
public class Amount implements ValueObject<Amount>, Comparable<Amount> {

    private static final long serialVersionUID = -2197617621274551405L;
    private final BigDecimal value;

    /**
     * Instantiates a new Amount.
     *
     * @param value the value
     */
    public Amount(BigDecimal value) {
        this.value = value;
    }

    public BigDecimal getValue() {
        return value;
    }

    @Override
    public int compareTo(Amount o) {
        return Comparator.comparing(Amount::getValue)
                .compare(this, o);
    }

    @Override
    public boolean sameValueAs(Amount other) {
        return other != null && this.value.equals(other.value);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Amount amount = (Amount) o;
        return sameValueAs(amount);
    }

    public Amount add(Amount addAmount) {
        return new Amount(this.value.add(addAmount.getValue()));
    }

    public Amount subtract(Amount subtractAmount) {
        return new Amount(this.value.subtract(subtractAmount.getValue()));
    }



    @Override
    public int hashCode() {
        return Objects.hash(value);
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}
